package com.tf.temper.controller;

import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.temper.entity.DeviceReturnEntity;
import com.tf.temper.entity.DeviceSettingEntity;
import com.tf.temper.entity.LogeventEntity;
import com.tf.temper.service.DeviceSettingService;
import com.tf.temper.service.LogeventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 上传温度
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "上传温度",description="DevuploadtemperController")
@RestController
@RequestMapping("/syattribute")
public class DevuploadtemperController extends BaseController
{
    @Autowired
    private LogeventService<LogeventEntity> logeventService;
    @Autowired
    private DeviceSettingService<DeviceSettingEntity> deviceSettingService;
    @ApiOperation("上传温度接口")
    @PostMapping("/{deviceid}")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public DeviceReturnEntity<String> syattribute(@PathVariable("deviceid") String deviceid, @RequestBody String feedbackentity)
    {
        LogeventEntity templogevent = new LogeventEntity();
        EntityWrapperHandle<DeviceSettingEntity> ewdevicentity = new EntityWrapperHandle<DeviceSettingEntity>();
        ewdevicentity.eq("deviceid", deviceid);
        DeviceSettingEntity curdevicesetting =  deviceSettingService.selectOne(ewdevicentity);
        if(feedbackentity!=null)
        {
            String temperaturejson = "0";
            String uuid = "";
            boolean mask = false;
            String nationCode = "";
            String openMsg = "";
            long screenTime = 0;
            String img = "";
            try
            {
                JSONObject feedbackentityJSON = new JSONObject(feedbackentity);
                JSONObject feedbackentityJSONinfo =  feedbackentityJSON.optJSONObject("Info");
                if(feedbackentityJSONinfo!=null)
                {
                    temperaturejson = feedbackentityJSONinfo.optString("temperature");
                    uuid = feedbackentityJSONinfo.optString("uuid");
                    mask = feedbackentityJSONinfo.optBoolean("mask");
                    nationCode = feedbackentityJSONinfo.optString("nationCode");
                    openMsg = feedbackentityJSONinfo.optString("openMsg");
                    screenTime = feedbackentityJSONinfo.optLong("screenTime");
                    img = feedbackentityJSONinfo.optString("img");
                }
            }
            catch (JSONException e)
            {

            }
            templogevent.setTemperature(temperaturejson);
            templogevent.setDeviceid(deviceid);
            if(curdevicesetting!=null)
            {
                templogevent.setUuid(curdevicesetting.getUuid());
            }
            else
            {
                templogevent.setUuid(uuid);
            }
            templogevent.setMask(mask==false?1:0);
            templogevent.setNationCode(nationCode);
            Double temp =  Double.valueOf(temperaturejson);
            if(  temp<37.30 )
            {
                templogevent.setOpenCode(1);
            }
            else
            {
                templogevent.setOpenCode(0);
            }
            templogevent.setOpenMsg(openMsg);
            templogevent.setUserType("user");
            templogevent.setScreenTime(screenTime);
            try
            {
                BASE64Decoder mdecoder = new BASE64Decoder();
                String format = "yyyy-MM-dd";
                SimpleDateFormat sdf = new  SimpleDateFormat(format);
                String curDate =  sdf.format(new Date());
                String imgpath = curDate+deviceid+System.currentTimeMillis()+".jpg";
                File file = new File("/images/"+imgpath);

                if (!file.exists())
                {
                    file.createNewFile();
                }
                byte[] buf = mdecoder.decodeBuffer(img);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(buf);
                fileOutputStream.flush();
                fileOutputStream.close();
                templogevent.setImgurl("http://healthy-backend-gcgs-cx-gc-20200902.ccsyun.vip/dataimages/"+imgpath);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            logeventService.insert(templogevent);
        }
        return  new DeviceReturnEntity<String>().ok("0");
    }
}